package com.umeng.soexample.mvp.persenter;

import android.util.Log;

import com.umeng.lib_core.mvp.presenter.BasePresenter;
import com.umeng.soexample.bean.AddCarEntity;
import com.umeng.soexample.bean.BaseEntity;
import com.umeng.soexample.bean.CarListEntity;
import com.umeng.soexample.bean.CollectionEntity;
import com.umeng.soexample.bean.SelGoodsValuesEntity;
import com.umeng.soexample.mvp.contract.CarListFragmentContract;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class CarListFragmentPresenter extends BasePresenter<CarListFragmentContract.CarListFragmentIView, CarListFragmentContract.CarListFragmentIModel> {
    @Inject
    public CarListFragmentPresenter(CarListFragmentContract.CarListFragmentIView carListFragmentIView, CarListFragmentContract.CarListFragmentIModel carListFragmentIModel) {
        super(carListFragmentIView, carListFragmentIModel);
    }

    /**
     * 获取用户购物车列表
     * @param userid
     */
    public void request(String userid, String goodsid){
        Map<String, String> map = new HashMap<>();
        JSONObject car_list = new JSONObject();
        try {
            car_list.put("userid", userid);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        map.put("car_list", car_list.toString());

        JSONObject sel_goods_values = new JSONObject();
        try {
            sel_goods_values.put("goodsid", goodsid);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        map.put("sel_goods_values", sel_goods_values.toString());

        m.request(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        v.showDialog();
                        disposable.add(d);
                    }

                    @Override
                    public void onNext(BaseEntity entity) {
                        v.hideDialog();
                        if (entity instanceof CarListEntity){
                            v.carListSuccess((CarListEntity) entity);
                        }else if (entity instanceof SelGoodsValuesEntity){
                            v.SelGoodsValuesSuccess((SelGoodsValuesEntity) entity);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        v.hideDialog();
                        v.showMessage(e.toString());
                    }

                    @Override
                    public void onComplete() {
                        v.hideDialog();
                    }
                });
    }

    /**
     * 加入购物车
     * @param userid
     * @param goodsid
     * @param shopname
     * @param shopcolor
     * @param shopsize
     * @param shopnum
     * @param shopprice
     */
    public void addCarRequest(String userid, String goodsid, String shopname, String shopcolor, String shopsize, String shopnum, String shopprice){
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("userid", userid);
            jsonObject.put("goodsid", goodsid);
            jsonObject.put("shopname", shopname);
            jsonObject.put("shopcolor", shopcolor);
            jsonObject.put("shopsize", shopsize);
            jsonObject.put("shopnum", shopnum);
            jsonObject.put("shopprice", shopprice);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        m.AddCarValues(jsonObject.toString()).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<AddCarEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        v.showDialog();
                        disposable.add(d);
                    }

                    @Override
                    public void onNext(AddCarEntity addCarEntity) {
                        Log.d("aaa","请求");
                        v.hideDialog();
                        v.AddCarSuccess(addCarEntity);
                    }

                    @Override
                    public void onError(Throwable e) {
                        v.hideDialog();
                        v.showMessage(e.toString());
                    }

                    @Override
                    public void onComplete() {
                        v.hideDialog();
                    }
                });
    }

    /**
     * 收藏商品
     */
    public void CollectionRequest(String userid, String goodsid){
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("userid", userid);
            jsonObject.put("goodsid", goodsid);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        m.CollectionValues(jsonObject.toString()).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<CollectionEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        v.showDialog();
                        disposable.add(d);
                    }

                    @Override
                    public void onNext(CollectionEntity entity) {
                        v.hideDialog();
                        v.CollectionSuccess(entity);
                    }

                    @Override
                    public void onError(Throwable e) {
                        v.hideDialog();
                        v.showMessage(e.toString());
                    }

                    @Override
                    public void onComplete() {
                        v.hideDialog();
                    }
                });


    }


}
